//
//  MLSearchResultsTableViewController.m
//  Medicine
//
//  Created by Visoport on 3/1/17.
//  Copyright © 2017年 Visoport. All rights reserved.
//

#import "MLSearchResultsTableViewController.h"
#import "JWSearcell.h"//搜索结果cell

#import "JWGRChankanVC.h"
#import "JWUserManager.h"
#import "JWGRUsermodel.h"
#import "SDUserModel.h"
@interface MLSearchResultsTableViewController ()
@property (nonatomic, strong) JWGRUsermodel *GRmodel;
@property (nonatomic, strong) NSMutableArray *GRarrylist;//研究生列表
@end

@implementation MLSearchResultsTableViewController

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.GRmodel=[JWUserManager Gme];
    self.edgesForExtendedLayout = UIRectEdgeNone;// 推荐使用

    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, Navigation_Bar_Height, ScreenWidth, ScreenHeight-Navigation_Bar_Height) style:UITableViewStyleGrouped];

    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(handleColorChange:) name:@"searchBarDidChange" object:nil];
}

-(void)handleColorChange:(NSNotification* )sender
{
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    
    NSString *text = sender.userInfo[@"searchText"];
    NSDictionary *dic=@{
                        @"jwid":JWid,
                        @"jwpassword":JWpassword,
                        @"searchname":text,
    };
    [[AFNetworkTool shareManager]postJSONWithUrl: JWSearch_list parmas:dic successData:^(id json) {
    //研究生数据
        [self.GRarrylist removeAllObjects];
        __weak typeof(self) weakslf = self;
        NSArray *GRarry=json[@"postgraduatelist"];
        for (int i=0; i<GRarry.count; i++){
            self.GRmodel=[JWGRUsermodel yy_modelWithJSON:GRarry[i]];
            [JWUserManager saveGR:self.GRmodel];
            [weakslf.GRarrylist addObject:weakslf.GRmodel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [EasyShowLodingView hidenLoding];
            [weakslf.tableView reloadData];
        });
      
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    } ];
    
    NSLog(@"%@", text);
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.GRarrylist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    JWSearcell *cell=[JWSearcell cellwithTableview:tableView];
    self.GRmodel=self.GRarrylist[indexPath.row];
    
    cell.nameLab.text = self.GRmodel.name;
    cell.specialLab.text=self.GRmodel.college;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.GRmodel=self.GRarrylist[indexPath.section];
     [JWUserManager saveGR:self.GRmodel];
    self.didSelectText([tableView cellForRowAtIndexPath:indexPath].textLabel.text);
    JWGRChankanVC *vc=[[JWGRChankanVC alloc]init];
    vc.title=[NSString stringWithFormat:@"%@的详情",self.GRmodel.name];
  [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.didSelectText(@"");
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  100;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *Foot=[[UIView alloc]init];
    Foot.backgroundColor=[UIColor whiteColor];
    
    return Foot;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *head=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];

    
    return head;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma -mark ———————————————————懒加载数组————————————————————
-(NSMutableArray *)GRarrylist{
    if(!_GRarrylist){
        _GRarrylist=[[NSMutableArray alloc]init];
    }
    return _GRarrylist;
}

@end
